home *** CD-ROM | disk | FTP | other *** search
/ Super PC 34 / Super PC 34 (Shareware).iso / spc / UTIL / DJGPP2 / V2 / DJDEV200.ZIP / include / sys / farptr.h < prev    next >
Encoding:
C/C++ Source or Header  |  1995-11-25  |  6.6 KB  |  235 lines

  1. /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
  2. /* Copyright (c) 1995 DJ Delorie.  Permission granted to use for any
  3.    purpose, provided this copyright remains attached and unmodified.
  4.  
  5.    THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  6.    IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  7.    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  8.  
  9. ╔═══════════════════════════════════════════════════════════════════════╗
  10. ║        Far Pointer Simulation Functions            ║
  11. ╚═══════════════════════════════════════════════════════════════════════╝
  12.  
  13. This file attempts to make up for the lack of a "far" keyword in GCC. 
  14. Although it doesn't provide access to far call APIs (like Windows), it
  15. does allow you to do far pointer data access without the overhead of
  16. movedata() or dosmemget/dosmemput().
  17.  
  18. You should *always* include this file when using these functions and
  19. compile with optimization enabled.  They don't exist as normal functions
  20. in any library, and they compile down to only a few opcodes when used
  21. this way.  They are almost as fast as native pointer operations, and
  22. about as fast as far pointers can get. 
  23.  
  24. If you don't use optimization, this file becomes prototypes for
  25. farptr.c, which generates real functions for these when not optimizing. 
  26. When optimizing, farptr.c compiles to nothing. 
  27.  
  28. There are two types of functions here - standalone and invariant.  The
  29. standalone functions take a selector and offset.  These are used when
  30. you need only a few accesses, time isn't critical, or you don't know
  31. what's in the %fs register.  The invariant ones don't take a selector,
  32. they only take an offset.  These are used inside loops and in
  33. time-critical accesses where the selector doesn't change.  To specify
  34. the selector, use the farsetsel() function.  That selector is used for
  35. all farns*() functions until changed. 
  36.  
  37. The farpoke* and farpeek* take selectors.
  38.  
  39. The farnspoke* and farnspeek* don't (note the `ns' for `no selector').
  40.  
  41. Warning: These routines all use the %fs register for their accesses. 
  42. GCC normally uses only %ds and %es, and libc functions (movedata,
  43. dosmemget, dosmemput) use %gs.  Still, you should be careful about
  44. assumptions concerning whether or not the value you put in %fs will be
  45. preserved across calls to other functions.  If you guess wrong, your
  46. program will crash.  Better safe than sorry. 
  47.  
  48. */
  49.  
  50. #ifndef __dj_include_sys_farptr_h_
  51. #define __dj_include_sys_farptr_h_
  52.  
  53. #ifdef __cplusplus
  54. extern "C" {
  55. #endif
  56.  
  57. #ifndef __dj_ENFORCE_ANSI_FREESTANDING
  58.  
  59. #ifndef __STRICT_ANSI__
  60.  
  61. #ifndef _POSIX_SOURCE
  62.  
  63. void _farpokeb(unsigned short, unsigned long, unsigned char);
  64. void _farpokew(unsigned short, unsigned long, unsigned short);
  65. void _farpokel(unsigned short, unsigned long, unsigned long);
  66. unsigned char _farpeekb(unsigned short, unsigned long);
  67. unsigned short _farpeekw(unsigned short, unsigned long);
  68. unsigned long _farpeekl(unsigned short, unsigned long);
  69. void _farsetsel(unsigned short);
  70. void _farnspokeb(unsigned long, unsigned char);
  71. void _farnspokew(unsigned long, unsigned short);
  72. void _farnspokel(unsigned long, unsigned long);
  73. unsigned char _farnspeekb(unsigned long);
  74. unsigned short _farnspeekw(unsigned long);
  75. unsigned long _farnspeekl(unsigned long);
  76.  
  77. extern __inline__ void
  78. _farpokeb(unsigned short selector,
  79.      unsigned long offset,
  80.      unsigned char value)
  81. {
  82.   __asm__ __volatile__ ("movw %w0,%%fs\n"
  83.       "    .byte 0x64 \n"
  84.       "    movb %b1,(%k2)"
  85.       :
  86.       : "rm" (selector), "qi" (value), "r" (offset));
  87. }
  88.  
  89. extern __inline__ void
  90. _farpokew(unsigned short selector,
  91.      unsigned long offset,
  92.      unsigned short value)
  93. {
  94.   __asm__ __volatile__ ("movw %w0,%%fs \n"
  95.       "    .byte 0x64 \n"
  96.       "    movw %w1,(%k2)"
  97.       :
  98.       : "rm" (selector), "ri" (value), "r" (offset));
  99. }
  100.  
  101. extern __inline__ void
  102. _farpokel(unsigned short selector,
  103.      unsigned long offset,
  104.      unsigned long value)
  105. {
  106.   __asm__ __volatile__ ("movw %w0,%%fs \n"
  107.       "    .byte 0x64 \n"
  108.       "    movl %k1,(%k2)"
  109.       :
  110.       : "rm" (selector), "ri" (value), "r" (offset));
  111. }
  112.  
  113. extern __inline__ unsigned char
  114. _farpeekb(unsigned short selector,
  115.      unsigned long offset)
  116. {
  117.   unsigned char result;
  118.   __asm__ __volatile__ ("movw %w1,%%fs \n"
  119.       "    .byte 0x64 \n"
  120.       "    movb (%k2),%b0"
  121.       : "=q" (result)
  122.       : "rm" (selector), "r" (offset));
  123.   return result;
  124. }
  125.  
  126. extern __inline__ unsigned short
  127. _farpeekw(unsigned short selector,
  128.      unsigned long offset)
  129. {
  130.   unsigned short result;
  131.   __asm__ __volatile__ ("movw %w1, %%fs \n"
  132.       "    .byte 0x64 \n"
  133.       "    movw (%k2),%w0 \n"
  134.       : "=r" (result)
  135.       : "rm" (selector), "r" (offset));
  136.   return result;
  137. }
  138.  
  139. extern __inline__ unsigned long
  140. _farpeekl(unsigned short selector,
  141.      unsigned long offset)
  142. {
  143.   unsigned long result;
  144.   __asm__ __volatile__ ("movw %w1,%%fs\n"
  145.       "    .byte 0x64\n"
  146.       "    movl (%k2),%k0"
  147.       : "=r" (result)
  148.       : "rm" (selector), "r" (offset));
  149.   return result;
  150. }
  151.  
  152. extern __inline__ void
  153. _farsetsel(unsigned short selector)
  154. {
  155.   __asm__ __volatile__ ("movw %w0,%%fs"
  156.       :
  157.       : "rm" (selector));
  158. }
  159.  
  160. extern __inline__ void
  161. _farnspokeb(unsigned long offset,
  162.      unsigned char value)
  163. {
  164.   __asm__ __volatile__ (".byte 0x64\n"
  165.       "    movb %b0,(%k1)"
  166.       :
  167.       : "qi" (value), "r" (offset));
  168. }
  169.  
  170. extern __inline__ void
  171. _farnspokew(unsigned long offset,
  172.      unsigned short value)
  173. {
  174.   __asm__ __volatile__ (".byte 0x64\n"
  175.       "    movw %w0,(%k1)"
  176.       :
  177.       : "ri" (value), "r" (offset));
  178. }
  179.  
  180. extern __inline__ void
  181. _farnspokel(unsigned long offset,
  182.      unsigned long value)
  183. {
  184.   __asm__ __volatile__ (".byte 0x64\n"
  185.       "    movl %k0,(%k1)"
  186.       :
  187.       : "ri" (value), "r" (offset));
  188. }
  189.  
  190. extern __inline__ unsigned char
  191. _farnspeekb(unsigned long offset)
  192. {
  193.   unsigned char result;
  194.   __asm__ __volatile__ (".byte 0x64\n"
  195.       "    movb (%k1),%b0"
  196.       : "=q" (result)
  197.       : "r" (offset));
  198.   return result;
  199. }
  200.  
  201. extern __inline__ unsigned short
  202. _farnspeekw(unsigned long offset)
  203. {
  204.   unsigned short result;
  205.   __asm__ __volatile__ (".byte 0x64\n"
  206.       "    movw (%k1),%w0"
  207.       : "=r" (result)
  208.       : "r" (offset));
  209.   return result;
  210. }
  211.  
  212. extern __inline__ unsigned long
  213. _farnspeekl(unsigned long offset)
  214. {
  215.   unsigned long result;
  216.   __asm__ __volatile__ (".byte 0x64\n"
  217.       "    movl (%k1),%k0"
  218.       : "=r" (result)
  219.       : "r" (offset));
  220.   return result;
  221. }
  222.  
  223. #endif /* !_POSIX_SOURCE */
  224. #endif /* !__STRICT_ANSI__ */
  225. #endif /* !__dj_ENFORCE_ANSI_FREESTANDING */
  226.  
  227. #ifndef __dj_ENFORCE_FUNCTION_CALLS
  228. #endif /* !__dj_ENFORCE_FUNCTION_CALLS */
  229.  
  230. #ifdef __cplusplus
  231. }
  232. #endif
  233.  
  234. #endif /* !__dj_include_sys_farptr_h_ */
  235.